/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.model.Corrector;
import br.com.foxline.model.Enterprise;
import br.com.foxline.service.CorrectorService;
import br.com.foxline.service.EnterpriseService;
import br.com.foxline.service.SaleService;
import br.com.foxline.service.UserService;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerReportSale implements Serializable {

    private Enterprise enterprise;
    private Corrector corrector;
    private Date startDate;
    private Date endDate;
    @EJB
    private SaleService saleService;
    @EJB
    private CorrectorService correctorService;
    @EJB
    private UserService userService;
    @EJB
    private EnterpriseService enterpriseService;
    private boolean usesCurrentUser = false;
    private boolean isCorrector = true;

    @PostConstruct
    public void init() {
        this.enterprise = new Enterprise();
        this.corrector = new Corrector();

        Corrector c = null;
        try {
            c = correctorService.findCorrectorByUser(userService.getCurrentUser());
        } catch (Exception e) {
        }
        if (c == null) {
            isCorrector = false;
        } else {
            usesCurrentUser = true;
            this.corrector = c;
        }
    }

    public void reportSales(){
        saleService.reportsSales(enterprise, corrector, startDate, endDate);
    }
    
    public void balanceSales(){
        saleService.reportsSales(enterprise, corrector, startDate, endDate);
    }
    
    
    public List<Enterprise> autocompleteEnterprise(String query) {
        return enterpriseService.autocomplete(query);
    }

    public List<Corrector> autocompleteCorrector(String query) {
        return correctorService.autocomplete(query);
    }

    public void loadCorrector() {
        if (usesCurrentUser) {
            this.corrector = correctorService.findCorrectorByUser(this.userService.getCurrentUser());
        } else {
            this.corrector = new Corrector();
        }
    }

    public Corrector getCorrector() {
        return corrector;
    }

    public void setCorrector(Corrector corrector) {
        this.corrector = corrector;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public boolean isIsCorrector() {
        return isCorrector;
    }

    public void setIsCorrector(boolean isCorrector) {
        this.isCorrector = isCorrector;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public boolean isUsesCurrentUser() {
        return usesCurrentUser;
    }

    public void setUsesCurrentUser(boolean usesCurrentUser) {
        this.usesCurrentUser = usesCurrentUser;
    }
}
